/**
 *   Copyright 2010-2012 GreenSoft
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neosystem.dao;

import java.io.Serializable;
import java.util.List;

import ar.com.greensoft.neosystem.dao.searching.SortCriteria;
import ar.com.greensoft.neosystem.search.SearchFilter;

/**
 * Generic Data Access Object Pattern for JPA Entities
 * @author Jorge E. Villaverde
 * @date 08/07/2011
 * @version 1.0.0
 */
public interface GenericDao<T, PK extends Serializable> {
	/**
	 * Retrieve an object that was previously persisted to the database using the indicated id as primary key
	 **/ 
	T findById(PK id) throws DaoException;
	
	/**
	 * Retrieve all objects
	 * @return
	 */
	List<T> findAll() throws DaoException;

	/**
	 * @param firstResult
	 * @param maxResults
	 * @return List of T in the Repository
	 */
	List<T> findEntries(int startPosition, int maxResult) throws DaoException;
	
	/**
	 * Find an Entity by its name
	 * @param name
	 * @return
	 */
	T findByName(String name);	

	/**
	 * @param persistentObject
	 * @return
	 */
	T merge(T persistentObject) throws DaoException;

	/**
	* Save changes made to a persistent object.
	*/
	void persist(T transientObject) throws DaoException;
	
	/**
	 * @param persistentObject
	 */
	void refresh(T persistentObject) throws DaoException;
	
	/**
	* Remove an object from persistent storage in the database
	*/
	void remove(T persistentObject) throws DaoException;
	
	/**
	 * Count the number of entities
	 * @return
	 */
	long count() throws DaoException;
	
	/**
	 * @param searchFilter
	 * @param startPosition
	 * @param maxResult
	 * @return List of T in the Repository
	 * @throws DaoException
	 */
	List<T> findEntriesBySearchFilter(SearchFilter searchFilter, int startPosition, int maxResult) throws DaoException;
	
	/**
	 * Count the number of entities
	 * @param searchFilter
	 * @return
	 * @throws DaoException
	 */
	long countBySearchFilter(SearchFilter searchFilter) throws DaoException;
	
	// Extented methods used for searching with sotr criteria

	/**
	 * @param startPosition
	 * @param maxResult
	 * @param sortCriterias
	 * @return
	 * @throws DaoException
	 */
	List<T> findEntries(int startPosition, 
			int maxResult,
			List<SortCriteria> sortCriterias) throws DaoException;
	
	/**
	 * @param searchFilter
	 * @param startPosition
	 * @param maxResult
	 * @param sortCriterias
	 * @return
	 * @throws DaoException
	 */
	List<T> findEntriesBySearchFilter(SearchFilter searchFilter, 
			int startPosition, 
			int maxResult,
			List<SortCriteria> sortCriterias) throws DaoException;
	
}